﻿using System.Linq.Expressions;
using XLZF.MicroServices.IRepository;
using XLZF.MicroServices.Models;

namespace XLZF.MicroServices.Repository;

public class ClothingClassDictRepository : IClothingClassDictRepository
{
    private static ModelMigration.XLZFContext context = new ModelMigration.XLZFContext();

    private IRepository<Clothing_Class_Dict> _repository = new Repository<Clothing_Class_Dict>(context);

    public async Task<int> Add(Clothing_Class_Dict model)
    {
        return await _repository.Add(model);  
    }

    public int Del(Clothing_Class_Dict model)
    {
        return _repository.Remove(model);
    }

    public int Edit(Clothing_Class_Dict model)
    {
        return _repository.Update(model);
    }

    public Clothing_Class_Dict Get(string ID)
    {
        return _repository.GetModel(p => p.ID == ID);
    }

    public List<Clothing_Class_Dict> GetLists()
    {
        return _repository.GetAll().ToList();
    }

    public Page<Clothing_Class_Dict> GetPageData(string name, bool isAsc, int pageIndex, int pageSize)
    {
        Page<Clothing_Class_Dict>? data = null;

        if (string.IsNullOrEmpty(name))
        {
            data = _repository.SearchFor(pageIndex, pageSize, null, isAsc, e => e.CreateDate);
        }
        else
        {
            //条件表达式
            Expression<Func<Clothing_Class_Dict, bool>> predicate = e => string.IsNullOrWhiteSpace(name) || e.ClassName.Equals(name);

            data = _repository.SearchFor(pageIndex, pageSize, predicate, isAsc, e => e.CreateDate);
        }

        return data;
    }
}
